package com.darren.befriend.fragments;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.darren.befriend.HomeActivity;
import com.darren.befriend.R;
import com.darren.befriend.utilities.Utilities;

public class SettingsFragment extends ListFragment {

	private OnSettingClickListener mListener;

	public static SettingsFragment create() {
		SettingsFragment fragment = new SettingsFragment();
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		ViewGroup view = (ViewGroup) inflater.inflate(
				R.layout.layout_sliding_menu_list, null);
		view.setBackgroundResource(R.color.white);
		view.setPadding(0, 0, 0, 0);
		return view;

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		int screenHeight = Utilities.getScreenHeight(getActivity());
		int titleHeight = getResources().getDimensionPixelSize(
				R.dimen.comm_size_70dp);
		int rowHeight = getResources().getDimensionPixelSize(
				R.dimen.comm_size_70dp);
		int rows = (screenHeight - titleHeight) / rowHeight;

		ArrayList<String> items = new ArrayList<String>();
		items.add(getString(R.string.contact));
		items.add(getString(R.string.logout));
		if (items.size() < rows) {
			for (int i = items.size(); i < rows; i++) {
				items.add(i, "");
			}
		}

		SettingAdapter settingAdapter = new SettingAdapter(getActivity(), items);
		setListAdapter(settingAdapter);
		if (getActivity() instanceof HomeActivity) {
			((HomeActivity) getActivity()).setLeftImage(R.drawable.icon_menu);
			((HomeActivity) getActivity()).showRightButtonImg(false);
		}
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			mListener = (OnSettingClickListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement OnSettingClickListener");
		}
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		if (mListener != null) {
			mListener.onClickItem(position);
		}
	}

	public interface OnSettingClickListener {
		public void onClickItem(int position);
	}

	public class SettingAdapter extends BaseAdapter {
		private Context mContext;
		private ArrayList<String> mStringArr;

		public SettingAdapter(Context context, ArrayList<String> strArr) {
			mContext = context;
			mStringArr = strArr;
		}

		@Override
		public int getCount() {
			return mStringArr.size();
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View item;
			TextView itemTitle;
			LayoutInflater inflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			if (convertView == null) {
				item = inflater.inflate(R.layout.layout_setting_item, null);
				itemTitle = (TextView) item.findViewById(R.id.setting_item);
				itemTitle.setText(mStringArr.get(position));
				Utilities.setFontType(mContext, itemTitle);
			} else {
				item = (View) convertView;
			}
			return item;
		}

	}
}
